<template>
		<eui-page-frame topHeight="170" bottomHeight="88">
			<template #top>
				<view class="content-img">
					<eui-nav-bar title="单据详情"  color="#000000" />
				</view>
			</template>
			<eui-page-content>
				<eui-base-card>
					<eui-detail-show borderBottom="true" :items="Items" v-model="saleData" textAlign="right"></eui-detail-show>
				</eui-base-card>
				<eui-base-card>
					<view class="tipsFlex">
						<view class="tag-color"><text class="tag-text">异常</text></view>
						<text class="text-color">大于近30天日均销售5倍。</text>
					</view>
				</eui-base-card>
				<eui-base-card v-for="(item,index) in saleDetailData" :key="index">
					<eui-detail-show borderBottom="true"
					 :items="Items2" v-model="saleDetailData[index]"
						textAlign="right"></eui-detail-show>
				</eui-base-card>
			</eui-page-content>
			<template #bottom>
			  <view class="bt-button">
			  	<eui-button type="max2" title="审核" :disabled="!(saleData.c_status!=='3')" @click="auditBill" />
			  	<eui-button type="max" title="标记异常" @click="updateFlag" :disabled="!(saleData.c_status!=='3' && saleData.c_flag==='0')" />
			  </view>
			</template>
		</eui-page-frame>
</template>

<script setup lang="ts">
	import { ref } from 'vue'
	import { onLoad } from "@dcloudio/uni-app";
	import { useLoginStore } from '@/store/modules/useLoginStore';
	import  useService from '@/hooks/useService';
	import dayjs from 'dayjs';
	import { getRelatedName, getDictName, getDictSelectList } from '../components/RelatedName.js';
	const service = useService({ prefix: "apiApi" });
	const LoginStore = useLoginStore();
	const billId = ref('');
	const tabIndex = ref(0);
	const spaceLeft = ref(6);
	const saleData = ref({});
	const saleDetailData = ref([]);
	const Items = [
		{
			label: "单号",
			prop: "c_id",
			type: "text",
			element: {
			   
			},
		},
		{
			label: "总销售额",
			prop: "c_sale_value",
			type: "text",
			element: {
				precision: 2,
			},
		},
		{
			label: "制单时间",
			prop: "c_mk_dt",
			type: "text",
			element: {
			      mode: "YYYY-MM-DD",
			},
		},
		{
			label: "制单人",
			prop: "c_mk_username",
			type: "text",
			element: {
			   
			},
		},
	]
	const Items2 = [
		{
			label: "销售码",
			prop: "c_gds_code",
			type: "text",
			element: {
			   
			},
		},
		{
			label: "销售金额",
			prop: "c_sale_value",
			type: "text",
			element: {
				precision: 2,
			},
		},
		{
			label: "销售笔数",
			prop: "c_count",
			type: "text",
			element: {
			},
		},
		{
			label: "销售数量",
			prop: "c_n_sale",
			type: "text",
			element: {
			},
		},
		{
			label: "销售日期",
			prop: "c_sale_da",
			type: "text",
			element: {
				   mode: "YYYY-MM-DD",
			},
		},
	]

	onLoad(async({c_id}) => {
		billId.value= c_id;
		await GetBillItem()
	})
	//获取单据详情
	const GetBillItem = async()=>{
		const req = {
		                BillId: billId.value
		            };
		uni.showLoading({ title: "查询中...", mask: true});
		const res = await service.request('管易通销售补录单', 'EnjoyBillQuery', req);
		uni.hideLoading();
		if(res){
			saleData.value = res.ds.map(x=>{
				return{
					...x,
					c_sale_value:x.c_sale_value?.toFixed(2),
					c_mk_username: x.c_mk_userno?'['+x.c_mk_userno+']'+getRelatedName('rdsUsers', x.c_mk_userno):'',
					c_mk_dt: dayjs(x.c_mk_dt).format('YYYY-MM-DD HH:mm:ss')
				}
			})[0];
			//console.log('saleData.value',saleData.value)
			saleDetailData.value = res.ds1.map(x=>{
									 return {
										 ...x,
										 c_sale_value:x.c_sale_value?.toFixed(2),
										c_gds_code:'['+x.c_gcode+']'+ (x.c_gname||''),
										c_sale_da: dayjs(x.c_sale_da).format('YYYY-MM-DD')
									}
			})
		}
	}
	const updateFlag = async()=>{
		  console.log('标记异常')
		  const req = {
		                  BillId: billId.value
		              };
		  const res = await service.request('管易通销售补录单', 'UpdateSalesRecordFlag', req);
		  if(res){
			  uni.showToast({
			  	duration: 2000,
			  	icon: 'none',
			  	title: res 
			  });
			  setTimeout(()=>{
			  	 GetBillItem();
			  },2000);
		  }
	}
	const auditBill = async()=>{
		const req = {
		                BillId: billId.value,
						BillType:'DD1'
		            };
		const res = await service.request('管易通销售补录单', 'EnjoyBillAudit', req);
		if(res){
			uni.showToast({
				duration: 2000,
				icon: 'none',
				title: '审核成功' 
			});
			setTimeout(()=>{
				 GetBillItem();
			},2000);
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex: 1;
		flex-direction: column;
		background-color: #F2F2F2;
		padding: 0px !important
	}

	.content-img {
		background-color: #FFFFFF;
		background-size: 100% 420rpx;
		background-repeat: no-repeat;
		width: 100%;
	}

	.navigation {
		flex: 0 0 auto;
		margin-bottom: 5px;
	}

	.tag-color {
		background-color: rgba(255, 64, 0, 1);
		border-radius: 8px;
		padding: 1px 8px;
	}

	.tag-text {
		overflow-wrap: break-word;
		color: rgba(255, 255, 255, 1);
		font-size: 13px;
		font-weight: 500;
		text-align: left;
		white-space: nowrap;
		line-height: 20px;
	}

	.tipsFlex {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		padding-top: 10px;
	}

	.text-color {
		overflow-wrap: break-word;
		color: rgba(136, 136, 136, 1);
		font-size: 13px;
		text-align: left;
		white-space: nowrap;
		line-height: 20px;
		margin-left: 8px;
		margin-top: 1px;
	}

	.bt-button {
		background-color: #FFFFFF;
		padding: 10rpx 0rpx;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
</style>